package com.fgs.common.update;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.fgs.common.CommonDialog;
import com.fgs.common.R;
import com.fgs.common.api.CommonConfig;
import com.fgs.common.interf.OnDialogClickListener;

import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/4/9
 * @description
 **/
public class UpdateDialog extends CommonDialog {
    private Button updateButton;
    private TextView continueTextView;
    private TextView updateContentTextView;
    private TextView updateTextView;
    private TextView downloadTextView;
    private String content;
    private String downloadUrl;
    //是否强制升级
    private boolean isForce;
    private OnDialogClickListener onDialogClickListener;

    public UpdateDialog(@NonNull Context context) {
        super(context, R.style.style_select_dialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_update);
        setCancelable(false);
        setCanceledOnTouchOutside(false);

        Window window = this.getWindow();
        window.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);

        updateButton = findViewById(R.id.dialog_update_updateButton);
        continueTextView = findViewById(R.id.dialog_update_continueTextView);
        updateContentTextView = findViewById(R.id.dialog_update_updateContentTextView);
        updateContentTextView.setMovementMethod(ScrollingMovementMethod.getInstance());
        updateTextView = findViewById(R.id.dialog_update_appStoreTextView);
        downloadTextView = findViewById(R.id.dialog_update_downloadTextView);

        if (isForce) {
            continueTextView.setText("暂不升级，退出使用");
        } else {
            continueTextView.setText("暂不升级，继续使用");
        }

        if (!TextUtils.isEmpty(content)) {
            updateContentTextView.setText(content);
        }

        updateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (onDialogClickListener != null) {
                    onDialogClickListener.onPositiveClick();
                }
            }
        });

        continueTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (onDialogClickListener != null) {
                    onDialogClickListener.onNegativeClick(isForce);
                }
            }
        });
        updateTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                appStoreDownload();
            }
        });
        downloadTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                download();
            }
        });
    }


    public void setContent(String content) {
        this.content = content;
    }

    public void setDownloadUrl(String downloadUrl) {
        this.downloadUrl = downloadUrl;
    }

    public void setForce(boolean force) {
        isForce = force;
    }

    public void setOnDialogClickListener(OnDialogClickListener onDialogClickListener) {
        this.onDialogClickListener = onDialogClickListener;
    }

    public void appStoreDownload() {
        String url = CommonConfig.getInstance().commonOptions().getApkUrl();
        if (!TextUtils.isEmpty(url)) {
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getContext().startActivity(intent);
        }

    }

    public void download() {
        String url = downloadUrl;
        if (!TextUtils.isEmpty(url)) {
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getContext().startActivity(intent);
        }

    }
}
